<?php
$user = JFactory::getUser();
?>

<script type="text/javascript">
<!--

jQuery.noConflict();

jQuery().ready(function($){

	var msg = '';
		
	$('#btn_send').click(function(){

		if($('#system-message').is(":visible"))
			return false;
		
		var $fullname = $('#fullname').val();
		var $email = $('#email').val();
		var $comment = $('#comment').val();
		var $captcha = $('#recaptcha_response_field').val();
		var $recaptcha_challenge_field = $('#recaptcha_challenge_field').val();

		if(<?php if($user->guest): ?>$fullname == '' || $email == '' || $captcha == '' || <?php endif; ?>$comment == '')
		{
			setMessage('notice', '<?php echo JText::_('COM_JE_COMMENT_MSG_ERROR_INFO_INVALID');?>', true);			
			return false;
		}

		<?php if($user->guest): ?>
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(!emailReg.test($email))
		{
			setMessage('notice', '<?php echo JText::_('COM_JE_COMMENT_MSG_ERROR_EMAIL_INVALID');?>', true);			
			return false;
		}
		<?php endif; ?>
		
		$.post(
				'index.php?option=com_je_comment&task=save',
				{
					'fullname': $fullname, 
					'email': $email, 
					'comment': $comment, 
					'captcha': $captcha, 
					'recaptcha_challenge_field': $recaptcha_challenge_field,
					'item_id': $('#item_id').val(), 
					'component': $('#component').val(),
					'title': $('#title').val()
				},
				function(data)
				{
					if(data == 'INVALID')
					{
						setMessage('notice', '<?php echo JText::_('COM_JE_COMMENT_MSG_ERROR_SECURITY_CODE_INVALID');?>', true);
						Recaptcha.reload();
					}
					else if(data == 'ERROR')
					{
						msg = '<?php echo JText::_('COM_JE_COMMENT_MSG_NOTICE_ERROR');?>';
						setMessage('notice', msg, true);						
					}
					else
					{
						$('#notification-msg').removeClass('notice');

						//var captchaImage = '<?php echo JRoute::_('index.php?option=com_je_comment&task=get_image&'.session_name().'='.session_id().'&rand='.rand(1, 1000)); ?>';

						msg = '<?php echo JText::_('COM_JE_COMMENT_MSG_SUCCESS');?>';						
						setMessage('message', msg, false);
						
						$('#fullname').val('').focus();
						$('#email').val('');
						$('#comment').val('');

						Recaptcha.reload();

						<?php 
						$params = JComponentHelper::getParams('com_je_comment');
						if($params->get('auto_approved')):
						?>
						refresh();
						<?php endif;?>

						setTimeout(function(){
							$('#system-message').hide('slow');
						}, 5000);
					}
				}
			);

		
	});

	<?php /*
	$('#refresh-captcha').click(function(){
		var captchaImage = '<?php echo JRoute::_('index.php?option=com_je_comment&task=get_image&'.session_name().'='.session_id(), false); ?>&rand='+Math.random();
		$('#comment-captcha').attr('src', captchaImage);
	});
	*/ ?>

	function setMessage(css, msg, auto_hide)
	{
		$('#system-message').show('slow');
		$('#notification-msg').addClass(css);
		$('#message-content').html(msg);

		if(auto_hide)
		{			
			setTimeout(function(){
				$('#system-message').hide('slow');
			}, 2000);
		}
	}
});

//-->
</script>

	<div id="ac-title">
    	<div id="ac-sendcomment">
	       <h2><?php echo JText::_('COM_JE_COMMENT_SEND_COMMENT');?></h2>
	        
	        <dl id="system-message" style="display: none; width: 98%;">
	         	<dd id="notification-msg" class="message">
		        	<ul>
		        		<li id="message-content">&nbsp;</li>
		        	</ul>
		        </dd>
	        </dl>
	        
	    	<form action="#" method="post" name="frm-comment" id="frm-comment">
	    	
				<?php if($user->guest): ?>
				<div class="title"><?php echo JText::_('COM_JE_COMMENT_FULLNAME');?></div>
				<div><input type="text" class="input" name="fullname" id="fullname" /></div>
				<div class="title"><?php echo JText::_('COM_JE_COMMENT_EMAIL');?></div>
				<div><input type="text" class="input" name="email" id="email" /></div>
				
				<?php /*
				<div class="title">
				<?php echo JText::_('COM_JE_COMMENT_SECURITY_CODE');?><br />
				<img id="comment-captcha"
					src="index.php?option=com_je_comment&task=get_image&<?php echo session_name()?>=<?php echo session_id()?>&rand=<?php echo rand(0, '1000'); ?>" />
				<br />
				
				<span id="refresh-captcha" style="cursor: pointer;"><?php echo JText::_('COM_JE_COMMENT_REFRESH');?></span></div>
				<div><input type="text" class="input" name="captcha" id="captcha" /></div>
				
				*/ ?>
				<div class="title">
					<?php echo JText::_('COM_JE_COMMENT_SECURITY_CODE');?><br />
					<?php
					require_once(JPATH_ROOT . DS . 'jelibs' . DS . 'classes' . DS . 'recaptchalib.php');
					$publickey = CFG_RECAPTCHA_PUBLIC_KEY;
					echo recaptcha_get_html($publickey);
			        ?>
			        <div class="clr"></div>
				</div>
				<?php endif;?>
				
				<div class="title"><?php echo JText::_('COM_JE_COMMENT_YOUR_COMMENT');?></div>
	            <div>
	            	<textarea class="txtarea" name="comment" id="comment" rows="5" cols="50"></textarea>
	            </div>
	            <div>
	            	<input type="button" id="btn_send" class="submit" value="<?php echo JText::_('COM_JE_COMMENT_BUTTON_SEND');?>" />
	            </div>
	            
		        <input type="hidden" name="item_id" id="item_id" value="<?php echo $itemId; ?>" />
		        <input type="hidden" name="component" id="component" value="<?php echo $component; ?>" />
		        <input type="hidden" name="title" id="title" value="<?php echo $title; ?>" />
	        </form>
    	</div>
    </div>
    <div class="clr"></div>